// 主要的应用程序JavaScript文件

// 页面管理
function showPage(pageId) {
    // 隐藏所有页面
    $('.page-content').removeClass('active');
    
    // 显示目标页面
    $('#' + pageId).addClass('active');
    
    // 更新导航状态
    $('.nav-item').removeClass('active');
    $('[data-page="' + pageId + '"]').addClass('active');
    
    // 更新页面标题和面包屑
    updatePageInfo(pageId);
    
    // 如果是管理页面，自动加载人脸列表
    if (pageId === 'management') {
        loadFaceList();
    }
}

function updatePageInfo(pageId) {
    const pageInfo = {
        dashboard: { title: '仪表板', breadcrumb: '系统概览' },
        recognition: { title: '人脸识别', breadcrumb: '识别服务' },
        video: { title: '视频检测', breadcrumb: '视频服务' },
        management: { title: '人脸管理', breadcrumb: '库管理' },
        upload: { title: '人脸入库', breadcrumb: '数据录入' },
        debug: { title: '调试工具', breadcrumb: '问题诊断' }
    };
    
    $('#pageTitle').text(pageInfo[pageId].title);
    $('#pageBreadcrumb').text(pageInfo[pageId].breadcrumb);
}

// 文件上传处理
function setupFileUpload(uploadAreaId, inputId) {
    const uploadArea = document.getElementById(uploadAreaId);
    const input = document.getElementById(inputId);
    
    uploadArea.addEventListener('click', () => input.click());
    
    uploadArea.addEventListener('dragover', (e) => {
        e.preventDefault();
        uploadArea.classList.add('dragover');
    });
    
    uploadArea.addEventListener('dragleave', () => {
        uploadArea.classList.remove('dragover');
    });
    
    uploadArea.addEventListener('drop', (e) => {
        e.preventDefault();
        uploadArea.classList.remove('dragover');
        const files = e.dataTransfer.files;
        if (files.length > 0) {
            input.files = files;
            updateUploadArea(uploadArea, files[0]);
        }
    });
    
    input.addEventListener('change', (e) => {
        if (e.target.files.length > 0) {
            updateUploadArea(uploadArea, e.target.files[0]);
        }
    });
}

function updateUploadArea(uploadArea, file) {
    const icon = uploadArea.querySelector('.file-upload-icon i');
    const text = uploadArea.querySelector('.file-upload-text');
    const hint = uploadArea.querySelector('.file-upload-hint');
    
    icon.className = 'fas fa-check-circle';
    icon.style.color = 'var(--success-color)';
    text.textContent = file.name;
    hint.textContent = `文件大小: ${(file.size / 1024 / 1024).toFixed(2)} MB`;
}

// 加载状态管理
function showLoading(buttonId) {
    const button = document.getElementById(buttonId);
    const originalText = button.innerHTML;
    button.innerHTML = '<span class="loading"></span> 处理中...';
    button.disabled = true;
    return originalText;
}

function restoreButton(buttonId, originalText) {
    const button = document.getElementById(buttonId);
    button.innerHTML = originalText;
    button.disabled = false;
}

// 结果显示
function showResult(containerId, message, isSuccess = true) {
    const container = document.getElementById(containerId);
    const className = isSuccess ? 'result-success' : 'result-error';
    const icon = isSuccess ? 'fas fa-check-circle' : 'fas fa-exclamation-circle';
    
    container.innerHTML = `<p><i class="${icon}"></i> ${message}</p>`;
    container.className = 'result-container ' + className;
    
    setTimeout(() => {
        container.style.opacity = '0';
        setTimeout(() => {
            container.innerHTML = '';
            container.className = '';
            container.style.opacity = '1';
        }, 500);
    }, 5000);
}

// 重置上传区域
function resetUploadArea(uploadAreaId) {
    const uploadArea = document.getElementById(uploadAreaId);
    const icon = uploadArea.querySelector('.file-upload-icon i');
    const text = uploadArea.querySelector('.file-upload-text');
    const hint = uploadArea.querySelector('.file-upload-hint');
    
    icon.className = 'fas fa-cloud-upload-alt';
    icon.style.color = '';
    text.textContent = '点击或拖拽文件到此处';
    hint.textContent = '支持 JPG, PNG, JPEG 格式，最大 2MB';
}

// 显示成功提示
function showSuccessToast(message) {
    // 移除已存在的提示
    $('.success-toast').remove();
    
    const toast = $(`
        <div class="success-toast">
            <i class="fas fa-check-circle"></i> ${message}
        </div>
    `);
    
    $('body').append(toast);
    
    // 3秒后自动移除
    setTimeout(() => {
        toast.fadeOut(300, function() {
            $(this).remove();
        });
    }, 3000);
}

// 更新统计信息
function updateStats() {
    // 这里可以添加实时统计更新逻辑
    $('#todayRecognition').text(Math.floor(Math.random() * 50) + 10);
    $('#successRate').text('95%');
}

// 初始化
$(document).ready(function() {
    // 设置文件上传
    setupFileUpload('recognitionUploadArea', 'compareFaceFile');
    setupFileUpload('uploadUploadArea', 'faceFile');
    
    // 导航点击事件
    $('.nav-item').click(function() {
        const pageId = $(this).data('page');
        showPage(pageId);
    });
    
    // 点击模态框外部关闭
    $(document).on('click', function(e) {
        if ($(e.target).closest('#imageModal').length === 0 && !$(e.target).closest('.face-item').length) {
            closeModal();
        }
        
        // 点击删除确认对话框外部关闭
        if ($(e.target).closest('#deleteConfirmModal').length === 0 && !$(e.target).closest('.face-delete-btn').length) {
            closeDeleteModal();
        }
    });
    
    // ESC键关闭模态框
    $(document).on('keydown', function(e) {
        if (e.key === 'Escape') {
            closeModal();
            closeDeleteModal();
        }
        
        // 删除确认对话框的键盘快捷键
        if ($('#deleteConfirmModal').is(':visible')) {
            if (e.key === 'Enter') {
                e.preventDefault();
                confirmDelete();
            } else if (e.key === 'Escape') {
                e.preventDefault();
                closeDeleteModal();
            }
        }
    });
    
    // 初始化统计
    updateStats();
    
    // 页面加载动画
    $('.system-container').hide().fadeIn(1000);
}); 